var useFiles = [];

useFiles.push('uikit');
useFiles.push('uikit.css');
useFiles.push('easyui');
useFiles.push('easyui.css');
useFiles.push('easyui-icon.css');
useFiles.push('page.css');
useFiles.push('easyui-locale');

seajs.use(useFiles, function() {
	$(initPage);
});

function initPage() {
	$('a[href="#ok"]').on('click', ok_click);
	
	$('a[href="#reset"]').on('click', reset_click);
	
	$('a[href="#back"]').on('click', back_click);
	
	loadParentIdTree();
}

function loadParentIdTree() {
	var url = fand.webContextUrl + '/admin/configuration/dictionary/dictionaryTy/tree.json';
	
	$.messager.progress();
	
	$.post(url, function(data) {
		$.messager.progress('close');
		
		if (data.message == undefined) {
			if (data.dataList != null && data.dataList.length > 0) {
				$('input[name="dictionary.typeId"]').combotree({
					required : false
				}).combotree('loadData', data.dataList);
			}
			
			return;
		}
		
		$.messager.alert('提示消息', '加载数据字典类型下拉树失败:' + data.message, 'error');
	});
}

function ok_click() {
	$.messager.progress();
	
	$('form').form('submit', {
	    onSubmit : function() {
		    var isValid = $(this).form('validate');
		    
		    if (!isValid) {
			    $.messager.progress('close');
		    }
		    
		    var typeId = $('input[name="dictionary.typeId"]').val();
		    
		    if (typeId == '') {
			    $.messager.progress('close');
			    
			    $.messager.alert('提示消息', '类型为必选项', 'warning');
			    
			    return false;
		    }
		    
		    return isValid;
	    },
	    success : function(data) {
		    $.messager.progress('close');
		    
		    var jsonData = $.parseJSON(data);
		    
		    if (jsonData.message != undefined) {
			    $.messager.alert('提示消息', '提交失败:' + jsonData.message, 'error');
			    
			    return;
		    }
		    
		    $.messager.alert('提示消息', '提交成功', 'info', function() {
			    parent.$('.easyui-datagrid').datagrid('reload');
			    
			    back_click();
		    });
	    }
	});
}

function reset_click() {
	$('form').each(function() {
		this.reset();
	});
}

function back_click() {
	parent.$('#windowForIframe').window('close');
}